<template>
	<view>
		<!-- #ifdef MP-PAYRCCARD -->
		<u-navbar title="卡充值" :is-back="false"></u-navbar>
		<!-- #endif -->
		<!-- #ifndef MP-PAYRCCARD -->
		<u-navbar title="卡充值"></u-navbar>
		<!-- #endif -->
		<cardlook :Card="Card"></cardlook>
		<view class="" style="padding-left: 30rpx; font-size: 32rpx">
			{{ $z.$t("充值") }}
		</view>
		<view class="recharge-content" style="padding: 30rpx">
			<!-- class="" -->
			<u-button type="primary" v-for="(item, index) in jelist" @click="undatafn(item)"
				:custom-style="{ color: '#2093f5', ...rechargeitem }">
				<view class="" :style="textindex == 0 ? 'font-size: 30rpx;line-height: 40rpx;' : ''"
					v-for="(text, textindex) in item.texts">
					{{ $z.$t(text) }}
				</view>
			</u-button>
		</view>

		<zmodal ref="zmodal"></zmodal>
	</view>
</template>

<script>
	// import settingvue from "@/mixins/setting.vue";
	import qs from "qs";
	export default {
		data() {
			return {
				Card: {},
				query: {},
				umodaldata: {},
				jelist: [],
				USearch: {},
				rechargeitem: {
					height: "130rpx",
					width: "200rpx",
					padding: 0,
					margin: "20rpx 0",
					display: "flex",
					flexDirection: "column",
					lineHeight: 1,
					justifyContent: "space-around",
					padding: "20rpx 0",
					fontSize: "24rpx",
					background: "none",
				},
			};
		},
		// mixins: [settingvue],
		onLoad(query) {
			//#ifdef MP-PAYRCCARD
			if (!query.printid) {
				let locationquery = window.location.search;
				locationquery = locationquery.replace("?", "");
				query = qs.parse(locationquery);
			}
			// #endif
			this.query = query;
			this.getdata();
		},
		methods: {
			async back() {
				this.$z.back();
			},
			async getdata() {
				let Card = await this.$z.request("/Card", {
					printid: this.query.printid,
				});

				if (Card.length == 0) {
					this.$refs.zmodal.init({
						content: this.$z.$t("未查询到此信息请退出重新扫一扫"),
						showTitle: false,
					});

					return;
				}
				Card = Card[0];
				if (Card.type == "6" && Card.yys != "1") {
					this.$u.toast(this.$z.$t("此卡为到期卡无法充值"));
					return;
				}
				if (Card.printid.indexOf("E") >= 0) {
					this.Card = Card;
				} else {
					let Cards = await this.$z.request("/Cards", {
						printid: this.query.printid,
					});

					if (Cards.length == 0) {
						this.$refs.zmodal.init({
							content: this.$z.$t("未查询到此信息请退出重新扫一扫"),
						});

						return;
					}
					Cards = Cards[0];
					Card.yfy = Cards.yfy || Card.yfy;
					Card.yzdys = Cards.yzdys || Card.yzdys;
					this.Card = {
						...Card,
					};
				}
				this.jelistfn();
			},
			async jelistfn() {
				let jelist = [];
				let USearch = await this.$store.dispatch("user/getuser");

				this.USearch = USearch;
				if (this.Card.type == 2) {
					let reqdata = {
						printid: this.query.printid,
					};

					let CardYH = await uni.$z.request("/CardYH", reqdata);
					CardYH = CardYH.filter((e) => {
						return e.pcs == this.Card.pcs;
					});

					jelist = CardYH;
					if (jelist.length == 0) {
						jelist = [{
								je: 500,
								type: 0,
							},
							{
								je: 1000,
								type: 0,
							},
							{
								je: 2000,
								type: 0,
							},
							{
								je: 5000,
								type: 0,
							},
							{
								je: 10000,
								type: 0,
							},
							{
								je: 20000,
								type: 0,
							},
						];
					}
					jelist.forEach((e)=> {
						e.texts = [];
						if (e.type == 0) {
							e.texts.push(`${Number(e.je) / 100}${this.$z.$t("元")}`);
						} else if (e.type == 1) {
							let je = Number(e.je) - Number(e.pari);

							e.texts.push(`${Number(e.je) / 100}${this.$z.$t("元")}`,
								`${this.$z.$t("售价")}:${je / 100}`);
							e.je = je;
						} else if (e.type == 2) {
							let je = Number(e.je) + Number(e.pari);
							e.texts.push(
								`${Number(e.je) / 100}${this.$z.$t("元")}`,
								`${this.$z.$t("充")}${Number(e.je) / 100}${this.$z.$t("得")}${je / 100}`
							);
							e.je = Number(e.je);
						}
					});
				} else if (this.Card.type == 1) {
					for (let i = 1; i <= 3; i++) {
						let je = (i * Number(this.Card.yfy)) / 100;
						jelist.push({
							texts: [`${je}${this.$z.$t("元")}`,
								`${je}${this.$z.$t("元")}${i * this.Card.yzdys}${this.$z.$t("次")}`
							],
							je: i * Number(this.Card.yfy),
						});
					}
				} else if (
					this.Card.type == 4 ||
					this.Card.type == 5 ||
					this.Card.type == 3
				) {
					for (let i = 1; i <= 3; i++) {
						let je = (i * Number(this.Card.yfy)) / 100;
						jelist.push({
							texts: [
								`${je}${this.$z.$t("元")}`,
								`${i * Number(this.Card.yzdys)}${this.$z.$t("月")}(${
                  this.Card.type == 5
                    ? (this.Card.ycs / 60).toFixed(1) + this.$z.$t("小时")
                    : this.Card.ycs + this.$z.$t("次")
                }/${this.$z.$t("月")})`,
							],
							je: i * Number(this.Card.yfy),
						});
					}
					if (Number(this.Card.yzdys) == 1) {
						let jesj = [{
								i: 6,
								text: "6" + this.$z.$t("月"),
							},
							{
								i: 12,
								text: "1" + this.$z.$t("年"),
							},
							{
								i: 24,
								text: "2" + this.$z.$t("年"),
							},
						];
						jesj.forEach((e) => {
							let je =
								(Number(this.Card.yfy) * e.i) / Number(this.Card.yzdys) / 100;
							jelist.push({
								texts: [
									`${je}${this.$z.$t("元")}`,
									`${e.text}(${
                    this.Card.type == 5
                      ? (this.Card.ycs / 60).toFixed(1) +  this.$z.$t("小时")
                      : this.Card.ycs + this.$z.$t("次")
                  }/${this.$z.$t("月")})`,
								],
								je: Number(this.Card.yfy) * e.i,
							});
						});
					}
				} else if (this.Card.type == 6) {
					jelist.push({
						texts: [
							`${this.Card.yfy / 100}${this.$z.$t("元")}`,
							`${this.Card.yfy / 100}${this.$z.$t("元")}${this.Card.yzdys}${this.$z.$t("月")}`,
						],
						je: Number(this.Card.yfy),
					});
				}
				if (jelist.length == 0) {
					jelist = [{
							je: 500,
							type: 0,
						},
						{
							je: 1000,
							type: 0,
						},
						{
							je: 2000,
							type: 0,
						},
						{
							je: 5000,
							type: 0,
						},
						{
							je: 10000,
							type: 0,
						},
						{
							je: 20000,
							type: 0,
						},
					];
					jelist.forEach(function(e) {
						e.texts = [];
						if (e.type == 0) {
							e.texts.push(`${Number(e.je) / 100}${this.$z.$t("元")}`);
						}
					});
				}

				console.log(jelist);
				this.jelist = jelist;
			},
			async undatafn(item) {
				let url = {
						weixin: "/WXCardxd",
						zfb: "/AlipayCard",
					},
					updata = {
						printid: this.Card.printid,
						je: item.je,
						uUser: this.USearch.uUser,
					};
				if (item.id) {
					updata.id = item.id;
				}
				this.$refs.zmodal.init({
					show: true,
					content: this.$z.$t("支付") + updata.je / 100 + `${this.$z.$t("元")}`,
					showTitle: false,
					showCancelButton: true,
					confirm: () => {
						if (
							this.Card.type == "0" ||
							this.Card.type == "1" ||
							this.Card.type == "2" ||
							this.Card.type == "4"
						) {
							this.$z.pay(url, updata).then(() => {
								this.back();
								this.$u.toast(this.$z.$t("操作成功"));
							});
						} else if (this.Card.type == "5") {
							if (this.Card.je == "0") {
								updata.b_jh = 1;
								this.$z.pay(url, updata).then(() => {
									this.back();
									this.$u.toast(this.$z.$t("操作成功"));
								});
							} else {
								this.umodaldata = {
									show: true,
									content: this.$z.$t("当月使用点击'是'；下月使用点击'否'"),
									showTitle: false,
									showCancelButton: true,
									confirmText: this.$z.$t("是"),
									cancelText: this.$z.$t("否"),
									updatafn: () => {
										updata.b_jh = 1;
										this.$z.pay(url, updata).then(() => {
											this.back();
											this.$u.toast(this.$z.$t("操作成功"));
										});
									},
									cancelfn: () => {
										updata.b_jh = 0;
										this.$z.pay(url, updata).then(() => {
											this.back();
											this.$u.toast(this.$z.$t(
												"操作成功"));
										});
									},
								};
							}
						} else if (this.Card.type == "3") {
							this.umodaldata = {
								show: true,
								content: this.$z.$t("当月使用点击'是'；下月使用点击'否'"),
								showTitle: false,
								showCancelButton: true,
								confirmText: this.$z.$t("是"),
								cancelText: this.$z.$t("否"),
								updatafn: () => {
									let t = uni.$z.timefn.getUTCtime(
										"",
										uni.$z.timefn.gettimemm(-1),
										60 * 60 * 24
									);
									if (t < 20) {
										this.umodaldata = {
											show: true,
											content: this.$z.$t(
												`本月已过去${t}天,确认马上使用`),
											showTitle: false,
											showCancelButton: true,
											confirmText: this.$z.$t("当月使用"),
											cancelText: this.$z.$t("下月使用"),
											updatafn: () => {
												updata.b_jh = 1;
												this.$z.pay(url, updata)
													.then(
														() => {
															this.back();
															this.$u.toast(
																this.$z
																.$t(
																	"操作成功"
																)
															);
														});
											},
											cancelfn: () => {
												updata.b_jh = 0;
												this.$z.pay(url, updata)
													.then(
														() => {
															this.back();
															this.$u.toast(
																this.$z
																.$t(
																	"操作成功"
																)
															);
														});
											},
										};
									} else {
										updata.b_jh = 1;
										this.$z.pay(url, updata).then(() => {
											this.back();
											this.$u.toast(this.$z.$t(
												"操作成功"));
										});
									}
									console.log(t, uni.$z.timefn.gettimemm(-1));
								},
								cancelfn: () => {
									updata.b_jh = 0;
									this.$z.pay(url, updata).then(() => {
										this.back();
										this.$u.toast(this.$z.$t("操作成功"));
									});
								},
							};
						} else {
							this.$z.pay(url, updata).then(() => {
								this.back();
								this.$u.toast(this.$z.$t("操作成功"));
							});
						}
					},
				});
			},
		},
	};
</script>

<style lang="scss">
	.recharge-content {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 40rpx;

		&::after {
			content: "";
			width: 200rpx;
		}
	}
</style>